﻿using System.Linq;
using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;

namespace ComeSeeThis_MVC2.Controllers
{
    public class VehicleTypeController : Controller
    {
        // ReSharper disable InconsistentNaming
        readonly VehicleTypeRepository vehicleTypeRepository = new VehicleTypeRepository();
        // ReSharper restore InconsistentNaming

        #region Get Methods

        //
        // GET: /VehicleType/
        public ActionResult Index()
        {
            var vehicleTypes = vehicleTypeRepository.GetAllVehicleTypes().ToList();
            return View(vehicleTypes);
        }

        public ActionResult Details(int id)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);

            if (vehicleType == null) return View("VehicleTypeNotFound");

            return View(new VehicleTypesFormView(vehicleType));
        }

        public ActionResult NotAuthorizedToEdit(int id)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);

            if (vehicleType == null) return View("VehicleTypeNotFound");

            return View(vehicleType);
        }

        public ActionResult Submitted(int id)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);

            if (vehicleType == null) return View("VehicleTypeNotFound");

            return View(vehicleType);
        }

        #endregion

        #region Edit Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Edit(int id)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);

            // Check if this user can edit this
            //if (CheckPermission(vehicleType.SubmittedBy)) return RedirectToAction("NotAuthorizedToEdit", new { id = vehicleType.VehicleTypeId });

            return vehicleType == null ? View("VehicleTypeNotFound") : View(new VehicleTypeFormView(vehicleType));
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);

            // Check if this user can edit this
            //if (CheckPermission(vehicleType.SubmittedBy)) return RedirectToAction("NotAuthorizedToEdit", new { id = vehicleType.VehicleTypeId });

            if (TryUpdateModel(vehicleType, "VehicleType"))
            {
                vehicleTypeRepository.Save();
                return RedirectToAction("Index");
            }
            return View(new VehicleTypeFormView(vehicleType));
        }

        #endregion

        #region Create Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Create()
        {
            var vehicleType = new VehicleType();

            return View(new VehicleTypeFormView(vehicleType));
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Create(VehicleType vehicleType)
        {
            if (ModelState.IsValid)
            {
                if (User.IsInRole("SiteAdmin") || User.IsInRole("GearingAdmin"))
                {
                    vehicleType.Approved = true;
                }
                else
                {
                    vehicleType.Approved = false;
                }
                vehicleType.SubmittedBy = User.Identity.Name;

                vehicleTypeRepository.Add(vehicleType);
                vehicleTypeRepository.Save();

                return RedirectToAction("Submitted", new { id = vehicleType.VehicleTypeId });
            }
            return View(new VehicleTypeFormView(vehicleType));
        }

        #endregion

        #region Delete Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Delete(int id)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);
            if (vehicleType == null) return View("VehicleTypeNotFound");

            // Check if this user can edit this
            //if (CheckPermission(vehicleType.SubmittedBy)) return RedirectToAction("NotAuthorizedToEdit", new { id = vehicleType.VehicleTypeId });
            return View(vehicleType);
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Delete(int id, string confirmButton)
        {
            var vehicleType = vehicleTypeRepository.GetVehicleType(id);
            if (vehicleType == null) return View("VehicleTypeNotFound");
            
            // Check if this user can edit this
            //if (CheckPermission(vehicleType.SubmittedBy)) return RedirectToAction("NotAuthorizedToEdit", new { id = vehicleType.VehicleTypeId });

            vehicleTypeRepository.Delete(vehicleType);
            vehicleTypeRepository.Save();

            return RedirectToAction("Index");
        }

        #endregion

        // Check permissions
        //private bool CheckPermission(string submittedBy)
        //{
        //    // Check if this user can edit this
        //    if (User.IsInRole("SiteAdmin")) return true;
        //    return submittedBy.ToLower() == User.Identity.Name.ToLower();
        //}
    }
}
